<?php

declare(strict_types=1);

namespace Plugin\Attractions\AttractionManagement\Controller;

use Common\CommonController;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\Swagger\Annotation\HyperfServer;
use Plugin\Attractions\AttractionManagement\Repository\AttractionTypeRepository;

#[HyperfServer(name: 'http')]
#[Controller('attractions/attraction-management')]
class TypeController extends CommonController
{ 
    #[Inject]
    private AttractionTypeRepository $repository;

    #[GetMapping('listTypes')]
    public function listTypes()
    {
        $data = $this->valid([
            'id' => 'integer',
            'name' => 'string|max:64',
            'parentId' => 'integer',
            'sort' => 'integer|in:0,1',
        ], []);
        return $this->success($this->repository->list($data)->toArray());
    }

    #[PostMapping('addType')]
    public function addType()
    {
        $data = $this->valid([
            'type' => 'required|integer|in:0,1',
            'name' => 'required|string|max:64',
            'description' => 'string|max:255',
            'sort' => 'integer|min:0|max:200',
            'parentId' => 'integer',
            'level' => 'integer',
        ], []);
        return $this->success($this->repository->create($data)->toArray());
    }

    #[GetMapping('deleteType')]
    public function deleteType()
    {
        $data = $this->valid(['id' => 'required|regex:/^\d+(,\d+)*$/'], []);
        $ids = explode(',', $data['id']);
        return $this->success($this->repository->deleteById($ids));
    }

    #[PostMapping('updateType')]
    public function updateType()
    {
        $data = $this->valid([
            'id' => 'required|regex:/^\d+(,\d+)*$/',
            'type' => 'required|integer|in:0,1',
            'name' => 'required|string|max:64',
            'description' => 'string|max:255',
            'sort' => 'integer|min:0|max:200',
            'parentId' => 'integer',
            'level' => 'integer',
        ], []);
        $ids = explode(',', $data['id']);
        return $this->success($this->repository->updateById($ids, $data));
    }
}